package com.lab.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lab.gulimall.product.entity.AttrEntity;
import com.lab.gulimall.product.service.AttrAttrgroupRelationService;
import com.lab.gulimall.product.service.AttrService;
import com.lab.gulimall.product.service.CategoryService;
import com.lab.gulimall.product.vo.AttrGroupRelationVo;
import com.lab.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lab.gulimall.product.entity.AttrGroupEntity;
import com.lab.gulimall.product.service.AttrGroupService;
import com.lab.common.utils.PageUtils;
import com.lab.common.utils.R;

import javax.swing.text.html.parser.Entity;


/**
 * 属性分组
 *
 * @author hyson
 * @email 1228841297@qq.com
 * @date 2021-12-16 11:21:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();
    }

    //product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId) {
        //1、查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> vos  = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        //2、查出每个属性分组的所有属性
        return R.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息，显示完整的父类信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //这里拿到了属性的分组id的整一行数据
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//拿到当前的这一行的分类Id
        Long catelogId = attrGroup.getCatelogId();
        //根据分类Id拿到完整的分类Ids
        Long[] ids = categoryService.findCatelogIds(catelogId);
        attrGroup.setCatelogIds(ids);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @Autowired
    AttrService attrService;
    //product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationArr(attrgroupId);
        return R.ok().put("data", entities);
    }

    //product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    private R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
//        for (int i = 0; i < vos.length; i++) {
//            System.out.println(vos[i]);
//        }
        //json的数据vos
        attrService.deleteRelation(vos);
        return R.ok();
    }

    //product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params) {
        PageUtils pages = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", pages);
    }
}
